package com.naiterui.ehp.bs.doctor.vo.education;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * 宣教定制分页

 * @version 2017年3月29日 by wangchuan
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class EduPageVO<T> implements Serializable {

    private static final long serialVersionUID = 4370011634572970770L;

    public static final int MIN_PAGESIZE = 20;
    public static final int MAX_PAGESIZE = 200;

    // 分页参数
    protected int pageNo = 1;
    protected int pageSize = MIN_PAGESIZE;

    /** 查询开始的宣教类型，用于列表拼接查询 */
    private Integer queryType;

    /** 上一页结束的序号 */
    private Integer startIndex;

    // 返回结果
    protected List<T> result = Collections.emptyList();

    protected int totalCount = -1;

    // 构造函数
    public EduPageVO() {
        super();
    }

    public EduPageVO(final int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获得当前页的页号,序号从1开始,默认为1.
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * 设置当前页的页号,序号从1开始,低于1时自动调整为1.
     */
    public void setPageNo(final int pageNo) {
        this.pageNo = pageNo;
        if (pageNo < 1) {
            this.pageNo = 1;
        }
    }

    /**
     * 获得每页的记录数量,默认为10.
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 设置每页的记录数量,超出MIN_PAGESIZE与MAX_PAGESIZE范围时会自动调整.
     */
    public void setPageSize(final int pageSize) {
        this.pageSize = pageSize;
        if (pageSize > MAX_PAGESIZE) {
            this.pageSize = MAX_PAGESIZE;
        }
    }

    /**
     * 取得页内的记录列表.
     */
    public List<T> getResult() {
        return result;
    }

    public void setResult(final List<T> result) {
        this.result = result;
    }

    /**
     * 取得总记录数,默认值为-1.
     */
    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(final int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * 根据pageSize与totalCount计算总页数,默认值为-1.
     */
    public int getTotalPages() {
        if (totalCount < 0) {
            return -1;
        }

        int count = totalCount / pageSize;
        if (totalCount % pageSize > 0) {
            count++;
        }
        return count;
    }

    /**
     * 是否还有下一页.
     */
    public boolean isHasNext() {
        return pageNo + 1 <= getTotalPages();
    }

    /**
     * 取得下页的页号,序号从1开始.
     */
    public int getNextPage() {
        if (isHasNext()) {
            return pageNo + 1;
        } else {
            return pageNo;
        }
    }

    /**
     * 是否还有上一页.
     */
    public boolean isHasPre() {
        return pageNo - 1 >= 1;
    }

    /**
     * 取得上页的页号,序号从1开始.
     */
    public int getPrePage() {
        if (isHasPre()) {
            return pageNo - 1;
        } else {
            return pageNo;
        }
    }

    /**
     * 计算分页起始元素位置
     */
    public static int getStartOfPage(int pageNo, int pageSize) {
        return (pageNo - 1) * pageSize;
    }

    public Integer getQueryType() {
        return queryType;
    }

    public void setQueryType(Integer queryType) {
        this.queryType = queryType;
    }

    public Integer getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(Integer startIndex) {
        this.startIndex = startIndex;
    }


}
